package com.rr.learningdemo.flink;

import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaProducer;

import java.util.Properties;

/**
 * @author RR
 * @date 2021/7/21 23:54
 */
public class SinkKafakaTest {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        Properties properties = new Properties();
        //配置好kafka的地址，记得开端口，虚拟机图省事直接把防火墙关了
        properties.setProperty("bootstrap.servers", "192.168.31.112:9092");
        //要监听的topic是test，消费一些简单的string消息
        FlinkKafkaConsumer<String> consumer = new FlinkKafkaConsumer<>("first", new SimpleStringSchema(), properties);
        //从最早开始消费
        consumer.setStartFromEarliest();
        DataStream<String> stream = env
                .addSource(consumer);
        //addSink操作，可以将输入的数据源通过sink输出出去，这里是输出到另一个kafka的topic里面
        stream.addSink(new FlinkKafkaProducer<String>("192.168.31.112:9092", "second", new SimpleStringSchema()));
        stream.print();
        //stream.map();
        env.execute();
    }
}
